package com.jxc.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jxc.dao.BaseDaoI;
import com.jxc.model.po.Goods;
import com.jxc.model.po.OrderReturnlist;
import com.jxc.model.po.OrderReturnlistItem;
import com.jxc.model.po.Repertory;
import com.jxc.model.po.User;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.VOrderReturnlist;
import com.jxc.model.vo.VOrderReturnlistItem;
import com.jxc.service.OrderReturnServiceI;
@Service("orderReturnService")
public class OrderReturnServiceImpl implements OrderReturnServiceI {

	@Autowired
	private BaseDaoI<OrderReturnlist> orderReturnDao;
	@Autowired
	private BaseDaoI<Goods> goodsDao;
	@Autowired
	private BaseDaoI<OrderReturnlistItem> itemDao;

	@Override
	public DataGrid datagrid(VOrderReturnlist vOrderReturnlist) {
		DataGrid dg = new DataGrid();
		dg.setRows(this.changeModel(this.find(vOrderReturnlist)));
		dg.setTotal(this.total(vOrderReturnlist));
		return dg;
	}

	private List<VOrderReturnlist> changeModel(
			List<OrderReturnlist> orderReturnlists) {
		List<VOrderReturnlist> vOrderReturnlists = new ArrayList<VOrderReturnlist>();
		if (orderReturnlists != null && orderReturnlists.size() > 0) {
			for (OrderReturnlist orderReturnlist : orderReturnlists) {
				VOrderReturnlist vOrderReturnlist = new VOrderReturnlist();
				BeanUtils.copyProperties(orderReturnlist, vOrderReturnlist);
				if (orderReturnlist.getRepertory() != null) {
					Repertory repertory = orderReturnlist.getRepertory();
					vOrderReturnlist.setRid(repertory.getId());
					vOrderReturnlist.setRname(repertory.getName());
				}
				if (orderReturnlist.getUser() != null) {
					User user = orderReturnlist.getUser();
					vOrderReturnlist.setUid(user.getId());
					vOrderReturnlist.setUname(user.getUsername());
				}
				vOrderReturnlists.add(vOrderReturnlist);
			}
		}
		return vOrderReturnlists;
	}

	private List<OrderReturnlist> find(VOrderReturnlist vOrderReturnlist) {
		String hql = "from OrderReturnlist t where t.isDeleted = false ";

		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vOrderReturnlist, hql, values);

		if (vOrderReturnlist.getSort() != null
				&& vOrderReturnlist.getOrder() != null) {
			hql += " order by " + vOrderReturnlist.getSort() + " "
					+ vOrderReturnlist.getOrder();
		}
		return orderReturnDao.find(hql, values, vOrderReturnlist.getPage(),
				vOrderReturnlist.getRows());
	}

	private Long total(VOrderReturnlist vOrderReturnlist) {
		String hql = "select count(*) from OrderReturnlist t where t.isDeleted=false ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vOrderReturnlist, hql, values);
		return orderReturnDao.count(hql, values);
	}

	private String addWhere(VOrderReturnlist vOrderReturnlist, String hql,
			List<Object> values) {
		if(vOrderReturnlist.getId()!=null){
			hql += " and t.id = ? ";
			values.add(vOrderReturnlist.getId());
		}
		if (vOrderReturnlist.getDateStart() != null) {
			hql += " and t.date >= ? ";
			values.add(vOrderReturnlist.getDateStart());
		}
		if (vOrderReturnlist.getDateEnd() != null) {
			hql += " and t.date <= ? ";
			values.add(vOrderReturnlist.getDateEnd());
		}
		if (vOrderReturnlist.getRid() != null) {
			hql += " and t.repertory.id  = ? ";
			values.add(vOrderReturnlist.getRid());
		}
		return hql;
	}

	@Override
	public void delete(String ids) {
		if (ids != null) {
			for (String id : ids.split(",")) {
				if (!id.trim().equals("0")) {
					OrderReturnlist orderReturnlist = orderReturnDao.get(
							OrderReturnlist.class, Long.parseLong(id.trim()));
					if (orderReturnlist != null) {
						orderReturnlist.setIsDeleted(true);
						orderReturnDao.update(orderReturnlist);
					}
				}
			}
		}

	}

	@Override
	public void save(VOrderReturnlist vOrderReturnlist,String items) {
		OrderReturnlist orderReturnlist = new OrderReturnlist();
		BeanUtils.copyProperties(vOrderReturnlist, orderReturnlist);
		if (vOrderReturnlist.getRid() != null) {
			orderReturnlist.setRepertory(new Repertory(vOrderReturnlist
					.getRid()));
		}
		if (vOrderReturnlist.getUid() != null) {
			orderReturnlist.setUser(new User(vOrderReturnlist.getUid()));
		}
		orderReturnlist.setIsDeleted(false);
		this.orderReturnDao.save(orderReturnlist);
		String[] objs = items.split(",");
		Double total=0d;
		if(objs.length>0){
			for(int i=0;i<objs.length;){
				Goods goods = goodsDao.get(Goods.class, Long.parseLong(objs[i]));
				Integer num = Integer.parseInt(objs[i+1]);
				if(goods!=null&&num>0){
					OrderReturnlistItem item = new OrderReturnlistItem();
					item.setReturnPrice(goods.getBuyPrice());
					item.setGoods(goods);
					item.setNum(num);
					item.setOrderReturnlist(orderReturnlist);
					item.setTotal(num*goods.getBuyPrice());
					itemDao.save(item);
					total+=item.getTotal();
					goods.setAmount(goods.getAmount()-num);
					goods.setOrderReturnTotal(goods.getOrderReturnTotal()+num);
					goodsDao.update(goods);
				}
				i=i+2;
			}
		}
		orderReturnlist.setTotal(total);
		orderReturnDao.update(orderReturnlist);
	}

	@Override
	public List<VOrderReturnlistItem> getItems(VOrderReturnlist vOrderReturnlist) {
		String hql = "from OrderReturnlistItem t where t.orderReturnlist.id = ? ";
		List<Object> values = new ArrayList<Object>();
		values.add(vOrderReturnlist.getId());
		List<OrderReturnlistItem> items = itemDao.find(hql, values);
		return changeModelForItem(items);
	}
	private List<VOrderReturnlistItem> changeModelForItem(List<OrderReturnlistItem> items){
		List<VOrderReturnlistItem> results = new ArrayList<VOrderReturnlistItem>();
		if (items != null && items.size() > 0) {
			for (OrderReturnlistItem item : items) {
				VOrderReturnlistItem vOrderReturnlistItem = new VOrderReturnlistItem();
				BeanUtils.copyProperties(item, vOrderReturnlistItem);
				if (item.getGoods() != null) {
					Goods goods = item.getGoods();
					vOrderReturnlistItem.setGid(goods.getId());
					vOrderReturnlistItem.setGname(goods.getName());
					vOrderReturnlistItem.setBname(goods.getBrand().getName());
					vOrderReturnlistItem.setColor(goods.getColor());
					vOrderReturnlistItem.setGtname(goods.getGoodsType().getName());
					vOrderReturnlistItem.setSize(goods.getSize());
					vOrderReturnlistItem.setUnit(goods.getUnit());
					vOrderReturnlistItem.setGtype(goods.getType());
				}
				results.add(vOrderReturnlistItem);
			}
		}
		return results;
	}
	@Override
	public List<VOrderReturnlist> search(VOrderReturnlist vOrderReturnlist) {
		return changeModel(find(vOrderReturnlist));
	}
}
